﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Pansoft.Authority.Entities;

namespace Pansoft.HIME.Tools.AuthorityConfigFileEditor
{
    public partial class AuthorityItemForm : Form
    {
        public AuthorityItem AuthorityItem { get; private set; }

        public AuthorityItemForm(AuthorityItem authItem)
        {
            AuthorityItem = authItem;
            InitializeComponent();
            if(AuthorityItem.Id > 0)
            {
                _IdTextbox.Value = AuthorityItem.Id;
                _NameTextbox.Text = AuthorityItem.Name;
                _DataLevelTextbox.Value = AuthorityItem.DatasetLevel;
                _EnableCheckbox.Checked = AuthorityItem.Enable;
                _DescriptionTextbox.Text = AuthorityItem.Description;
            }
        }

        private void OkButtonClick(object sender, EventArgs e)
        {
            if (_IdTextbox.Value < 1)
            {
                AuthorityItem = AuthorityItem.Empty;
            }
            else
            {
                var item = new AuthorityItem()
                {
                    Id = (int) _IdTextbox.Value,
                    Name = _NameTextbox.Text,
                    DatasetLevel = (short) _DataLevelTextbox.Value,
                    Enable = _EnableCheckbox.Checked,
                    Description = _DescriptionTextbox.Text
                };
                AuthorityItem = item;
            }
            DialogResult = DialogResult.OK;
            Close();
        }

        private void CancelButtonClick(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }
    }
}
